<?php
namespace Manage\Controller;
use Common\Controller\ManageBaseController;
use Think\Model;
use Common\Model\ArticleModel;
use Common\Model\MemberModel;
use Common\Model\JiluModel;



class ShuaController extends ManageBaseController{
	/*
	 * 刷手会员列表
	 */
	public function lists($ajax=null,$status=null,$page=null,$rows=null) {
		$map = array(); $status_view = array('default'=>'所有','del'=>'已删除','forbid'=>'禁用','allow'=>'正常');
		//查询条件 处理
		$now_status = $status_view['default'];
		if (isset($status) && key_exists($status, MemberModel::$mystat)) { //指定查询状态
			$map['status'] = MemberModel::$mystat[$status];
			$now_status = $status_view[$status];
		}else {
			$map['status'] = array('EGT',0); //默认查询状态为未删除的数据
		}
		if ($page <= 0) {
			$page = 1;
		}
		if ($rows > 0) {
			$listRows = $rows;
		}else {
			$listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
		}
		
		//筛选条件
		$filter = I('param.');
		$map['cate'] = 2;
		if(!empty($filter['cid'])){ //文章所属分类
			$map['cate']=$filter['cid'];
			$this->assign('cate', $map['cate']);
		}
		if ($filter['keyword']) {
			$map['account'] = array('like','%'.$filter['keyword'].'%');
			$this->assign('keyword', $filter['keyword']);
		}
		/******************/
		if ($ajax==='y') {
			$model = new Model('Member');
			$total = $model->where($map)->count(); //总记录数
			$list = $model->where($map)->order('id desc')->page("$page,$listRows")->select();
			
			//list数据处理
			foreach ($list as &$v) {
				$v['time_type'] =date('Y-m-d H:i:s',$v['reg_time']); 
			}
			unset($v);		
// 			print_r($list);
			$list = array("total"=>(int)$total,"rows"=>$list);
			$this->ajaxReturn($list);
		}else {
			$this->assign('_page', $page);
			$this->assign('_rows', $listRows);
			$this->assign('now_status',$now_status); //当前页面筛选的状态
			
			// 记录当前列表页的cookie
			cookie(C('CURRENT_URL_NAME'),$_SERVER['REQUEST_URI']);
			$this->display();
		}
	}
	/*
	 * 删除用户
	 */
	public function delete($id) {
		$id = (int)$id;
		
		$model = new Model('Member');
		if (false === $model->where('`id`='.$id)->setField('status',-1)) {
			$this->error('更新失败,未知错误!');
		}
		$this->success('删除成功');
	}
	/*
	 * 编辑用户
	 */
	public function update() {
		$return = array('status'=>0,'info'=>'');
		$data = I('post.');
		$data['content'] = $_POST['content'];
		if ($data['id']<=0) {
			$return['info']='请先选择要更新的记录';
			$this->ajaxReturn($return);
		}
	
		$model = new MemberModel();
		//门店
		$data['picurl'] = $_FILES['thumb'];
		if (empty($_FILES['thumb']) || $data['picurl']['error']=='4') {
			unset($data['picurl']);
		}
	
		if (false === $model->myUpdate($data)) {
			$return['info'] = $model->getError();
		}else {
			$return['status'] = 1;
			$return['info'] = '更新成功';
		}
		$this->ajaxReturn($return);
	}
	/**
	 * status 0,1 切换审核用户
	 */
	public function state($id) {
		$id = (int)$id;	
		$member = new MemberModel();
		$info = $member->where("`id`=".$id)->field('id,status,parent_id')->find();//获取当前用户账户信息
		if(!empty($info['parent_id'])){//如果当前用户parent_id
			$model = new Model();
			$jilu = new JiluModel();
			$model->startTrans();
			
			if(false == $member->where("id = ".$info['parent_id'])->setInc("able_money",C("FANXIAN"))){//更改可用余额
				$model->rollback();
				$this->error('更新失败,未知错误!');
			}
			
			if (false == $member->where("id = ".$info['parent_id'])->setInc("total_money",C("FANXIAN"))){//更改总余额
				$model->rollback();
				$this->error('更新失败,未知错误!');
			}
			
			$tjdata = array();
			$tjdata['money'] = C("FANXIAN");
			$tjdata['remark'] = '推荐注册返现';
			$tjdata['createtime'] = time();
			$tjdata['cate'] = 6;
			$tjdata['status'] = 1;
			$tjdata['sid'] = $info['parent_id'];
			$tjdata['yue'] = $member->where("id = ".$info['parent_id'])->getField("total_money");
			if (false == $jilu->add($tjdata)){//添加推荐注册返现记录
				$model->rollback();
				$this->error('更新失败,未知错误!');
			}
			
			$newstate =  ($info['status'] > 0) ? '0' : '1';
			if (false === $member->where('`id`='.$id)->setField('status',$newstate)) {//更新用户状态
				$model->rollback();
				$this->error('更新失败,未知错误!');
			}
			$model->commit();$this->success('更新成功');
		}else{
			$newstate =  ($info['status'] > 0) ? '0' : '1';
			if (false === $member->where('`id`='.$id)->setField('status',$newstate)) {//更新用户状态
				$model->rollback();
				$this->error('更新失败,未知错误!');
			}$this->success('更新成功');
		}
	}
}